
import { installRenderHelpers, resolveSlots } from './render-helpers/index'
import { createElement } from '../vdom/create-element'
import { nextTick } from '../util/next-tick'

export let currentRenderingInstance = null

export function initRender(vm) {
  vm._vnode = null // the root of the child tree
  vm._staticTrees = null // v-once cached trees

  //提供给子类
  const options = vm.$options
  //普通插槽内容
  vm.$slots = resolveSlots(options._renderChildren)

  vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)


}


export function renderMixin(Vue) {
  installRenderHelpers(Vue.prototype)

  Vue.prototype.$nextTick = function (fn) {
    return nextTick(fn, this)
  };

  Vue.prototype._render = function () {
    const vm = this
    const { render, _parentVnode } = vm.$options

    if (_parentVnode) {

      vm.$scopedSlots = normalizeScopedSlots(
        _parentVnode.data.scopedSlots, //命名插槽
        vm.$slots //普通插槽元素
      )
    }


    vm.$vnode = _parentVnode
    // render self
    let vnode
    try {
      currentRenderingInstance = vm
      vnode = render.call(vm, vm)
    } catch (e) {
      console.error(e)
    } finally {
      currentRenderingInstance = null
    }
    vnode.parent = _parentVnode
    return vnode
  }
}



function normalizeScopedSlots(slots,normalSlots){
  return {
    ...slots,
    ...normalSlots
  }
}



